package com.zhanjixun.springcloud.userweb.controller;

import cn.hutool.crypto.SecureUtil;
import com.zhanjixun.springcloud.userweb.query.UserQuery;
import com.zhanjixun.springcloud.userweb.service.UserService;
import com.zhanjixun.springcloud.userweb.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author :zhanjixun
 * @date : 2019/8/25 15:53
 */
@RestController
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping("/")
    public ModelAndView index() {
        return new ModelAndView("index");
    }

    @GetMapping("/reg")
    public ModelAndView regPage() {
        return new ModelAndView("reg");
    }

    @PostMapping(value = "/reg")
    public R reg(@ModelAttribute UserQuery user) {
        user.setPassword(SecureUtil.md5(user.getPassword()));
        boolean add = userService.add(user);
        if (add) {
            return R.success();
        }
        return R.error("注册失败！");
    }

    @PostMapping(value = "/login")
    public R login(@ModelAttribute UserQuery user) {
        user.setPassword(SecureUtil.md5(user.getPassword()).toUpperCase());
        return R.success(userService.queryUserByAccountAndPassword(user));
    }
}
